<?php
namespace App\Repositories\Eloquent;
use App\Models\Article;
use App\Repositories\Contracts\ArticleInterface;
use Laracasts\Flash\Flash;

class ArticleServiceRepository implements ArticleInterface{
    public function all($perPage){
        return Article::orderBy('id','desc')->paginate($perPage);
    }
    public function searchKeyWords($data,$perPage){
        return Article::where('name','like',"%{$data['keywords']}%")->orWhere('editor','like',"%{$data['keywords']}%")->orderBy('id','desc')->paginate($perPage);
    }

    public function findById($id){
        return Article::find($id);
    }

    public function showId($id){
        $data = Article::find($id);
        if( $data ){
            Article::find($id)->increment('tips'); //自增
            return $data;
        }else{
            abort(404);
        }
    }

    public function createInfo($data){
        $data = Article::create([
            'name'          => $data['title'],
            'pid'           => $data['pid'],
            'source'        => $data['source'],
            'editor'        => $data['author'],
            'status'        => 0,
            'thump'         => $data['art_thumb'],
            'description'   => $data['description'],
            'content'       => $data['content'],
            'is_index'      => $data['isIndex'],
        ]);
        if( $data ){
            Flash::success('添加成功！');
        }else{
            Flash::errors('添加失败！');
        }
    }
    public function destroyById($id){
        $info = Article::destroy($id);
        if($info){
            $data = [
                'status' => 1,
                'msg'   => '文章删除成功！',
            ];
        }else{
            $data = [
                'status' => 0,
                'msg'   => '文章删除失败！',
            ];
        }
        return response()->json($data);
    }
    public function updateId($data,$id){
        return Article::where(['id'=>$id])->update([
            'name'          => $data['title'],
            'pid'           => $data['pid'],
            'source'        => $data['source'],
            'editor'        => $data['author'],
            'status'        => 0,
            'thump'         => $data['art_thumb'],
            'description'   => $data['description'],
            'content'       => $data['content'],
            'is_index'      => $data['isIndex'],
        ]);
    }

    /**
     * 文件上传
     * @param $data
     */
    public function upload($data){
        $data->get;
    }
}